package com.rljk.controller.backend;

import com.rljk.common.controller.BaseController;
import com.rljk.common.result.CodeMsg;
import com.rljk.common.result.Result;
import com.rljk.domain.common.PageData;
import com.rljk.redis.RedisService;
import com.rljk.service.UserManager;
import com.rljk.service.impl.AssetsService;
import com.rljk.service.impl.BriefIntroductionService;
import com.rljk.service.impl.WealthService;
import com.rljk.utils.AppUtil;
import com.rljk.utils.Tools;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * Author:   YASUO
 * Date:     2018/8/15 15:12
 * Description:
 */
@RequestMapping("backwealth")
@RestController
@Slf4j
public class WealthController extends BaseController {

    @Resource(name = "wealthService")
    private WealthService wealthService;

    @Resource(name = "userService")
    private UserManager userService;

    @Autowired
    RedisService redisService;

    @Resource(name = "assetsService")
    private AssetsService assetsService;

    @Resource(name = "briefintroductionService")
    private BriefIntroductionService introductionService;


    /**
     * 后台获取宝石获取排位
     * @Description
     * @Author zhaozhenhua
     * @date   2018年8月15日
     */
    @RequestMapping(value="/backgemrankredis")
    //@Encrypt
    public Object backGemRankRedis() {
        log.info("后台获取宝石获取排位");
        PageData pd = new PageData();
        pd = this.getPageData();
        log.info("请求参数："+pd.toString());
        Map<String,Object> map = new HashMap<>();
        try {
            if (Tools.checkKey("BACKGEMRANK", pd.getString("FKEY"))) {
                if (AppUtil.checkParam("useridParam",pd)){
                    //1.获取用户信息
                    String USER_ID = redisService.get(pd.getString("TOKEN"));
                    pd.put("USER_ID",USER_ID);
                    Set<String> gemRankCache = assetsService.getGemRankCache();
                    if(gemRankCache.size()==0||gemRankCache==null){
                        assetsService.setGemRankCache();
                        gemRankCache = assetsService.getGemRankCache();
                    }
                    log.info("gemRankCache:"+gemRankCache);

                    List<JSONObject> gemRank = new ArrayList<>();

                    int zrevrank =1;

                    for (String s : gemRankCache) {
                        JSONObject jsonobject = JSONObject.fromObject(s);
                        jsonobject.put("rank",zrevrank);
                        log.info("排名rank:"+zrevrank);
                        zrevrank++;
                        gemRank.add(jsonobject);
                    }

                    return Result.success(gemRank);
                }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            } else {
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }


}
